package oto

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-resty/resty/v2"
	"net/http"
	"strings"
)

type gusinx struct {
	ip   string
	R    *gin.Engine
	port string
	c    *resty.Client
}

// Newgus 创建一个新的gus服务单元
func Newgus() *gusinx {
	return &gusinx{
		ip:   "127.0.0.1",
		R:    nil,
		port: "9999",
		c:    nil,
	}
}

// LimitRequestBody  设置一个set的最大上限内存大小
func LimitRequestBody(maxBytes int64) gin.HandlerFunc {
	return func(c *gin.Context) {
		// 使用 http.MaxBytesReader 包装请求体
		c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes)
		c.Next()
	}
}

func CheckJSONContentType() gin.HandlerFunc {
	return func(c *gin.Context) {
		contentType := c.Request.Header.Get("Content-Type")
		if !strings.Contains(contentType, "application/json") {
			c.AbortWithStatusJSON(http.StatusUnsupportedMediaType, gin.H{
				"error": "只接受 JSON 格式的请求",
			})
			return
		}
		c.Next()
	}
}

func Test() {
	fmt.Println("hello,oto")
}

